﻿using KnockoutDemo.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc;

namespace MVVM.Controllers
{
    public class Product
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
    public class HomeController : Controller
    {
        // GET api/home
        public ActionResult Index()
        {
            return View(new List<Product>
            {
                new Product{ID=1,Name="test1"},
                new Product{ID=2,Name="test2"},
                new Product{ID=3,Name="test3"},
                new Product{ID=4,Name="test4"},
                new Product{ID=5,Name="test5"},
                new Product{ID=6,Name="test6"},
            });
        }

        public JsonResult GetProduct()
        {
            return Json(
            new List<Product>
            {
                new Product{ID=1,Name="test1"},
                new Product{ID=2,Name="test2"},
                new Product{ID=3,Name="test3"},
                new Product{ID=4,Name="test4"},
                new Product{ID=5,Name="test5"},
                new Product{ID=6,Name="test6"},
            
            }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Create()
        {
            return View();
        }

        public ActionResult CreateAjaxPost(PeopleModel entity)
        {
            return Json(new { obj = entity });
        }

        public ActionResult TestForm()
        {
            return View();
        }

        public JsonResult Get(int customerID)
        {
            // Get the customer ...
            Customer customer = new Customer
            {
                CustomerID = customerID,
                FirstName = "John",
                LastName = "Doe",
                IsMale = true,
                CountryID = 1
            };
            return Json(customer);
        }

        public JsonResult Add(Customer customer)
        {
            // Save the customer ...

            // return status message 
            var message = string.Format("Customer: {0} {1} Added. IsMale: {2} Age:{3}  CountryID: {4} ",
                                        customer.FirstName, customer.LastName, customer.IsMale.ToString(),
                                        customer.Age.ToString(), customer.CountryID.ToString());
            return Json(message);
        }

        public ActionResult TestArea()
        {
            return View();
        }
    }
}
namespace KnockoutDemo.Models
{
    public class PeopleModel
    {
        public string name { get; set; }
        public string address { get; set; }
        public string realName { get; set; }
        public int availableCountries { get; set; }
        public string like { get; set; }
        public string Email { get; set; }
        public string phone { get; set; }
        public int age { get; set; }
    }
    public class Customer
    {

        public int CustomerID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public bool IsMale { get; set; }
        public int Age { get; set; }
        public int CountryID { get; set; }
    }
}